<?php
	
	class formidable_jslayer {

		var $oForm = null;
		var $bLoadScriptaculous = FALSE;
		var $bLoadScriptaculousDragDrop = FALSE;
		var $bLoadScriptaculousBuilder = FALSE;
		var $bLoadtooltip = FALSE;

		function _init(&$oForm) {
			require_once(PATH_tslib . "class.tslib_pagegen.php");
			$this->oForm =& $oForm;
		}

		function _includeOnceLibs() {
			
			if(!isset($GLOBALS["FORMIDABLE_LIBS_INCLUDED"])) {

				if($this->minified() === TRUE) {
					$this->_includeMinifiedJs();
					$this->oForm->additionalHeaderData(
						"<!-- consider formidable core loaded after this line -->",
						"tx_ameosformidable_core",
						$bFirstPos = FALSE,
						$sBefore = FALSE,
						$sAfter = "tx_ameosformidable_minified"
					);
				} else {
					$this->_includePrototype();
					$this->_includeJSFramework();
					$this->oForm->additionalHeaderData(
						"<!-- consider formidable core loaded after this line -->",
						"tx_ameosformidable_core",
						$bFirstPos = FALSE,
						$sBefore = FALSE,
						$sAfter = "tx_ameosformidable_jsframework"
					);
				}

				$GLOBALS["FORMIDABLE_LIBS_INCLUDED"] = TRUE;
			}

			$this->_includeDebugStyles();
		}
		
		function minified() {
			return (
				intval($this->oForm->conf["minify."]["enabled"]) === 1 &&
				file_exists(
					$this->oForm->toServerPath(t3lib_extMgm::siteRelPath("ameos_formidable") . "res/jsfwk/minified/formidable.minified.js")
				)
			);
		}

		function gziped() {
			return (
				$this->minified() &&
				intval($this->oForm->conf["minify."]["gzip"]) === 1 &&
				file_exists(
					$this->oForm->toServerPath(t3lib_extMgm::siteRelPath("ameos_formidable") . "res/jsfwk/minified/formidable.minified.js.gz")
				)
			);
		}
		
		function _includeThisFormDesc() {

			$aConf = array(
				"sFormId"				=> $this->oForm->formid,
				"Misc" => array(
					"Urls" => array(
						"Ajax" => array(
							"event" => $this->oForm->_removeEndingSlash(t3lib_div::getIndpEnv("TYPO3_SITE_URL")) . "/index.php?eID=tx_ameosformidable&object=tx_ameosformidable&servicekey=ajaxevent"
						),
					),
					"MajixSpinner" => (($aSpinner = $this->oForm->_navConf("/meta/majixspinner")) !== FALSE) ? $aSpinner : array(),
				),
			);

			$sJson = $this->oForm->array2json($aConf);
			$sScript = <<<JAVASCRIPT

Formidable.Context.Forms["{$this->oForm->formid}"] = new Formidable.Classes.FormBaseClass(
	{$sJson}
);

JAVASCRIPT;
			
			if(isset($GLOBALS["BE_USER"]) && method_exists($GLOBALS["BE_USER"], "isAdmin") && $GLOBALS["BE_USER"]->isAdmin()) {
				
				if($this->oForm->bDebug) {

					$sScript .= <<<JAVASCRIPT

Formidable.f("{$this->oForm->formid}").Manager = {
	enabled: true,
	Xml: {
		path: "{$this->oForm->_xmlPath}"
	}
};

JAVASCRIPT;

				}
			}

			$this->oForm->attachInitTask(
				$sScript,
				"Form '" . $this->oForm->formid . "' instance description",
				"framework-init"
			);
		}
		
		function _includeDebugStyles() {
			if($this->oForm->bDebug) {					
				
				$sPath = t3lib_extMgm::siteRelPath("ameos_formidable");
				$this->oForm->additionalHeaderData(
					"<link rel='stylesheet' type='text/css' href='" . $sPath . "res/css/debug.css' />",
					"tx_ameosformidable_debugstyles"
				);
			}
		}

		function _includeMinifiedJs() {

			$this->_includeFormidablePath();

			if($this->gziped()) {
				$sPath = t3lib_div::getIndpEnv("TYPO3_SITE_URL") . t3lib_extMgm::siteRelPath("ameos_formidable") . "res/jsfwk/minified/formidable.minified.js.php";
			} else {
				$sPath = t3lib_div::getIndpEnv("TYPO3_SITE_URL") . t3lib_extMgm::siteRelPath("ameos_formidable") . "res/jsfwk/minified/formidable.minified.js";
			}

			$this->oForm->additionalHeaderData(
				"<script type=\"text/javascript\" src=\"" . $sPath . "\"></script>",
				"tx_ameosformidable_minified",
				TRUE
			);
		}

		function _includePrototype() {

			$sPath = t3lib_div::getIndpEnv("TYPO3_SITE_URL") . t3lib_extMgm::siteRelPath("ameos_formidable") . "res/jsfwk/prototype/prototype.js";

			$this->oForm->additionalHeaderData(
				"<script type=\"text/javascript\" src=\"" . $sPath . "\"></script>",
				"tx_ameosformidable_prototype",
				$bFirstPos = FALSE,
				$sBefore = FALSE,
				$sAfter = "tx_ameosformidable_jsframework"
			);

			// Lowpro event & dom prototype addon
			// http://www.danwebb.net/2006/9/3/low-pro-unobtrusive-scripting-for-prototype
			$sPath = t3lib_div::getIndpEnv("TYPO3_SITE_URL") . t3lib_extMgm::siteRelPath("ameos_formidable") . "res/jsfwk/prototype/addons/lowpro/lowpro.js";

			$this->oForm->additionalHeaderData(
				"<script type=\"text/javascript\" src=\"" . $sPath . "\"></script>",
				"tx_ameosformidable_lowpro",
				$bFirstPos = FALSE,
				$sBefore = FALSE,
				$sAfter = "tx_ameosformidable_prototype"
			);
			
			// Dean Edward's Base API for clean prototype inheritance
			$sPath = t3lib_div::getIndpEnv("TYPO3_SITE_URL") . t3lib_extMgm::siteRelPath("ameos_formidable") . "res/jsfwk/prototype/addons/base/Base.js";

			$this->oForm->additionalHeaderData(
				"<script type=\"text/javascript\" src=\"" . $sPath . "\"></script>",
				"tx_ameosformidable_prototype_base",
				$bFirstPos = FALSE,
				$sBefore = FALSE,
				$sAfter = "tx_ameosformidable_lowpro"
			);

			// JSON stringifier
			// http://www.thomasfrank.se/downloadableJS/jsonStringify.js
			$sPath = t3lib_div::getIndpEnv("TYPO3_SITE_URL") . t3lib_extMgm::siteRelPath("ameos_formidable") . "res/jsfwk/json/json.js";

			$this->oForm->additionalHeaderData(
				"<script type=\"text/javascript\" src=\"" . $sPath . "\"></script>",
				"tx_ameosformidable_json",
				$bFirstPos = FALSE,
				$sBefore = FALSE,
				$sAfter = "tx_ameosformidable_prototype_base"
			);

			$this->oForm->additionalHeaderData(
				"<!-- consider prototype loaded after this line -->",
				"tx_ameosformidable_prototype_fwk",
				$bFirstPos = FALSE,
				$sBefore = FALSE,
				$sAfter = "tx_ameosformidable_json"
			);
		}
		
		function _includeJSFramework() {
			$sPath = t3lib_div::getIndpEnv("TYPO3_SITE_URL") . t3lib_extMgm::siteRelPath("ameos_formidable") . "res/jsfwk/framework.js";

			$this->oForm->additionalHeaderData(
				"<script type=\"text/javascript\" src=\"" . $sPath . "\"></script>",
				"tx_ameosformidable_jsframework",
				$bFirstPos = FALSE,
				$sBefore = FALSE,
				$sAfter = "tx_ameosformidable_prototype_fwk"
			);
			
			$this->_includeFormidablePath();
		}

		function _includeFormidablePath() {
			$sPath = t3lib_div::getIndpEnv('TYPO3_SITE_URL') . t3lib_extmgm::siteRelPath("ameos_formidable");
			$sScript =<<<JAVASCRIPT
				
	Formidable.initialize({path: '{$sPath}'});

JAVASCRIPT;

			$this->oForm->attachInitTask(
				$sScript,
				"Framework Formidable.path initialization"
			);
		}

		function loadScriptaculous() {
			$this->bLoadScriptaculous = TRUE;
		}

		function loadScriptaculousDragDrop() {
			$this->loadScriptaculous();
			$this->bLoadScriptaculousDragDrop = TRUE;
		}

		function loadScriptaculousBuilder() {
			$this->bLoadScriptaculousBuilder = TRUE;
		}

		function loadTooltip() {
			$this->loadScriptaculous();
			$this->loadScriptaculousBuilder();
			$this->bLoadtooltip = TRUE;
		}

		function _loadScriptaculous() {
			if($this->bLoadScriptaculous === TRUE) {
				
				// scriptaculous
				$sPath = t3lib_div::getIndpEnv("TYPO3_SITE_URL") . t3lib_extMgm::siteRelPath("ameos_formidable") . "res/jsfwk/scriptaculous/scriptaculous.js";

				if($this->minified()) {
					$sNextAfter = "tx_ameosformidable_minified";
				} else {
					$sNextAfter = "tx_ameosformidable_prototype_fwk";
				}

				$this->oForm->additionalHeaderData(
					"<script type=\"text/javascript\" src=\"" . $sPath . "\"></script>",
					"tx_ameosformidable_scriptaculous",
					$bFirstPos = FALSE,
					$sBefore = FALSE,
					$sNextAfter
				);

				
				$sPath = t3lib_div::getIndpEnv("TYPO3_SITE_URL") . t3lib_extMgm::siteRelPath("ameos_formidable") . "res/jsfwk/scriptaculous/effects.js";

				$this->oForm->additionalHeaderData(
					"<script type=\"text/javascript\" src=\"" . $sPath . "\"></script>",
					"tx_ameosformidable_scriptaculous_effects",
					$bFirstPos = FALSE,
					$sBefore = FALSE,
					$sAfter = "tx_ameosformidable_scriptaculous"
				);

				$sNextAfter = "tx_ameosformidable_scriptaculous_effects";

				if($this->bLoadScriptaculousDragDrop === TRUE) {
					$sPath = t3lib_div::getIndpEnv("TYPO3_SITE_URL") . t3lib_extMgm::siteRelPath("ameos_formidable") . "res/jsfwk/scriptaculous/dragdrop.js";

					$this->oForm->additionalHeaderData(
						"<script type=\"text/javascript\" src=\"" . $sPath . "\"></script>",
						"tx_ameosformidable_scriptaculous_dragdrop",
						$bFirstPos = FALSE,
						$sBefore = FALSE,
						$sNextAfter
					);

					$sNextAfter = "tx_ameosformidable_scriptaculous_dragdrop";
				}

				if($this->bLoadScriptaculousBuilder === TRUE) {
					$sPath = t3lib_div::getIndpEnv("TYPO3_SITE_URL") . t3lib_extMgm::siteRelPath("ameos_formidable") . "res/jsfwk/scriptaculous/builder.js";

					$this->oForm->additionalHeaderData(
						"<script type=\"text/javascript\" src=\"" . $sPath . "\"></script>",
						"tx_ameosformidable_scriptaculous_builder",
						$bFirstPos = FALSE,
						$sBefore = FALSE,
						$sNextAfter
					);

					$sNextAfter = "tx_ameosformidable_scriptaculous_builder";
				}

				$this->oForm->additionalHeaderData(
					"<!-- consider scriptaculous loaded after this line -->",
					"tx_ameosformidable_scriptaculous_fwk",
					$bFirstPos = FALSE,
					$sBefore = FALSE,
					$sNextAfter
				);
			}
		}

		function _loadTooltip() {
			if($this->bLoadtooltip === TRUE) {

				// tooltip css
				$sPath = t3lib_div::getIndpEnv("TYPO3_SITE_URL") . t3lib_extMgm::siteRelPath("ameos_formidable") . "res/jsfwk/tooltip/tooltips.css";

				$this->oForm->additionalHeaderData(
					"<link rel=\"stylesheet\" type=\"text/css\" href=\"" . $sPath . "\" />",
					"tx_ameosformidable_tooltip_css",
					$bFirstPos = FALSE,
					$sBefore = FALSE,
					"tx_ameosformidable_scriptaculous_fwk"
				);

				// tooltip js
				$sPath = t3lib_div::getIndpEnv("TYPO3_SITE_URL") . t3lib_extMgm::siteRelPath("ameos_formidable") . "res/jsfwk/tooltip/tooltips.js";

				$this->oForm->additionalHeaderData(
					"<script type=\"text/javascript\" src=\"" . $sPath . "\"></script>",
					"tx_ameosformidable_tooltip_js",
					$bFirstPos = FALSE,
					$sBefore = FALSE,
					"tx_ameosformidable_tooltip_css"
				);
			}
		}
	}

	if (defined("TYPO3_MODE") && $TYPO3_CONF_VARS[TYPO3_MODE]["XCLASS"]["ext/ameos_formidable/api/class.jslayer.php"])	{
		include_once($TYPO3_CONF_VARS[TYPO3_MODE]["XCLASS"]["ext/ameos_formidable/api/class.jslayer.php"]);
	}
?>